package com.ruoyi.fun.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fun.domain.EcProductSpec;
import com.ruoyi.fun.service.IEcProductSpecService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品规格Controller
 * 
 * @author ruoyi
 * @date 2025-08-06
 */
@RestController
@RequestMapping("/fun/spec")
public class EcProductSpecController extends BaseController
{
    @Autowired
    private IEcProductSpecService ecProductSpecService;

    /**
     * 查询商品规格列表
     */
    @PreAuthorize("@ss.hasPermi('fun:spec:list')")
    @GetMapping("/list")
    public TableDataInfo list(EcProductSpec ecProductSpec)
    {
        startPage();
        Long orgId = getOrgId();
        // 103组织拥有不过滤的权限
        if (orgId != null && orgId == 103L) {
            orgId = null;
        }
        ecProductSpec.setOrgId(orgId);
        List<EcProductSpec> list = ecProductSpecService.selectEcProductSpecList(ecProductSpec);
        return getDataTable(list);
    }

    /**
     * 导出商品规格列表
     */
    @PreAuthorize("@ss.hasPermi('fun:spec:export')")
    @Log(title = "商品规格", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EcProductSpec ecProductSpec)
    {
        List<EcProductSpec> list = ecProductSpecService.selectEcProductSpecList(ecProductSpec);
        ExcelUtil<EcProductSpec> util = new ExcelUtil<EcProductSpec>(EcProductSpec.class);
        util.exportExcel(response, list, "商品规格数据");
    }

    /**
     * 获取商品规格详细信息
     */
    @PreAuthorize("@ss.hasPermi('fun:spec:query')")
    @GetMapping(value = "/{specId}")
    public AjaxResult getInfo(@PathVariable("specId") Long specId)
    {
        Long orgId = getOrgId();
        // 103组织拥有不过滤的权限
        if (orgId != null && orgId == 103L) {
            orgId = null;
        }
        return success(ecProductSpecService.selectEcProductSpecBySpecIdAndOrgId(specId, orgId));
    }

    /**
     * 新增商品规格
     */
    @PreAuthorize("@ss.hasPermi('fun:spec:add')")
    @Log(title = "商品规格", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EcProductSpec ecProductSpec)
    {
        ecProductSpec.setOrgId(getOrgId());
        return toAjax(ecProductSpecService.insertEcProductSpec(ecProductSpec));
    }

    /**
     * 修改商品规格
     */
    @PreAuthorize("@ss.hasPermi('fun:spec:edit')")
    @Log(title = "商品规格", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EcProductSpec ecProductSpec)
    {
        return toAjax(ecProductSpecService.updateEcProductSpec(ecProductSpec));
    }

    /**
     * 删除商品规格
     */
    @PreAuthorize("@ss.hasPermi('fun:spec:remove')")
    @Log(title = "商品规格", businessType = BusinessType.DELETE)
	@DeleteMapping("/{specIds}")
    public AjaxResult remove(@PathVariable Long[] specIds)
    {
        return toAjax(ecProductSpecService.deleteEcProductSpecBySpecIds(specIds));
    }
    /**
     * 根据specId和组织ID获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('fun:ecproductspec:query')")
    @GetMapping(value = "/{specId}/org/{orgId}")
    public AjaxResult getInfoByOrgId(@PathVariable("specId") Long specId, @PathVariable("orgId") Long orgId)
    {
        return success(ecProductSpecService.selectEcProductSpecBySpecIdAndOrgId(specId, orgId));
    }

    /**
     * 根据组织ID查询列表
     */
    @PreAuthorize("@ss.hasPermi('fun:ecproductspec:list')")
    @GetMapping("/org/{orgId}")
    public TableDataInfo listByOrgId(@PathVariable Long orgId)
    {
        startPage();
        EcProductSpec ecProductspec = new EcProductSpec();
        List<EcProductSpec> list = ecProductSpecService.selectEcProductSpecListByOrgId(ecProductspec, orgId);
        return getDataTable(list);
    }


}